/*
leetcode 128. 最长连续序列
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
		 unordered_set<int> hash;
		 for(auto x:nums) hash.insert(x);
		 int longest=0;
		 for(auto x:hash){
		 	if(hash.count(x-1)) continue;
		 	//int currentNum=x;
		 	int count=1;
		 	while(hash.count(x+1)){
		 		x++;
		 		count++;
			}
			longest=max(longest,count);
		 } 
		 return longest;
    }
};

//方法二：较麻烦  
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
		if(nums.size()==0) return 0;
		int longest=1;
		int count=1;
		sort(nums.begin(),nums.end());
		for(int i=1;i<nums.size();i++){
			if(nums[i]==nums[i-1]) continue;
			if((nums[i]-nums[i-1])==1){
				count++;
			}else{
				count=1;
			}
			longest=max(longest,count);
		}
		return longest;
    }
};